<?php
namespace app\index\controller;
use app\index\model\Base;
use think\Controller;
use think\Session;
use think\Db;
class   Login  extends Controller
{
    /*
     * 个人主页
     * */
    public function index(){

        return $this->fetch('show');
    }
    /*
     * 登录操作
     * */
    public function login(){
        $user=I('phoneNumber');
        $pwd=I('password');
        $res = Db::table('user')->where("phones='$user'")->find();
        if($res && !empty($user)){
            if(md5($pwd)==$res['password']){
                $this->redirect('Index/index');
            }else{
                return  2;
            }
        }else{
            return 1;
        }



    }
    /*
     * 退出登录清除用户信息
     * */
    public function loginOut(){
        Session::clear();
        return $this->fetch('login');
    }
    /*
     * ajax判断号码是否存在
     * */
    public function checkPhone(){
        $phonNumber=I('Phon');
        $type=I('type');
        $res = Db::table('user')->where("phones='$phonNumber'")->find();
            if ($res) {
                if($type=='login'){
                    return '';
                }else{
                    return '该用户已经注册';
                }

            }else {
                if($type=='login'&& !empty($phonNumber)) {
                    return '该用户未被注册过,请注册';
                }else{
                    return '';
                }
            }
        }
    /*
     * 个人资料页面
     *
     * */
    public function userinfo(){

        return $this->fetch('userinfo');
    }
    /*
     * 注册页面
     *
     * */
    public function register(){
        $phon=I('phoneNumber');
        $pwd=I('password');
        $code=I('yzm');
        $cookie=session('code');
        if(empty($phon)){
            return $this->fetch('register');
        }else if($code==$cookie){
            $add=new Base();
            $data=array(
                'phones'    =>$phon,
                'password' =>md5($pwd),
            );
           $res= $add->add('user',$data);
            if($res){
                return $this->fetch('login');
            }
        }else{
            $this->error('验证码错误');
        }

    }
    /*
     * 发送短信方法
     * */
    public function getMsg(){
         $phonNumber=I('Phone');
         $num=rand(1000,9999);
         session('code',$num);
        if(preg_match("/^1[34578]\d{9}$/",$phonNumber)){
            if($this->get($phonNumber,$num)){
               session('code',$num);
                return '短信发送成功';
            }else{
                return '链接超时发送失败';
            }
           }
    }

    /*
     *
     * 手机短信发送接口
     *
     * */
    public function get($phonNumber,$num){
        $ch = curl_init();
        /* 设置验证方式 */
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8'));

        /* 设置返回结果为流 */
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        /* 设置超时时间*/
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);

        /* 设置通信方式 */
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $data=array('apikey'=>'e7a3bb3b6a2ba5f3205513bbf7c23b22','mobile'=>$phonNumber,'text'=>"【云片网】您的验证码是".$num);
        $json_data = $this->send($ch,$data);
        $array = json_decode($json_data,true);
        echo '<pre>';print_r($array);

    }

    public function send($ch,$data){
        curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $result = curl_exec($ch);
        $error = curl_error($ch);
        return $result;
    }


}